﻿using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DotNetExtensions.AspNet;
using Habi.Solver.Common;
using Habi.Solver.Helpers;
using Habi.Solver.Models;
using Habi.Solver.Models.Resources;
using Habi.Solver.Services;
using Habi.Solver.ViewModels.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetExtensions.AspNet.DevExpress.Services;
using DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels;

namespace Habi.Solver.ViewModels
{
    public class ResourceViewModel
    {
        public virtual string SearchKey { get; set; }

        public virtual ResourceType Type { get; set; }

        public virtual IList<ResourceModel> Selected { get; set; }

        public virtual IList<ResourceModel> Resources { get; set; }

        protected IControlOperationService operationService { get { return this.GetService<IControlOperationService>(); } }
        protected IMessageService notifyService { get { return this.GetService<IMessageService>(); } }
        public IDocumentManagerService FlyoutDocumentManagerService
        {
            get { return this.GetService<IDocumentManagerService>("FlyoutDocumentManagerService"); }
        }
        protected IDocumentManagerService DocumentManagerService { get { return this.GetService<IDocumentManagerService>(); } }
        public ResourceViewModel()
        {
            ResourceContainer.Default.OnChanged += (e) =>
            {
                Load();
            };
        }

        public void Load()
        {
            Resources = ResourceContainer.Default.Resources.Values.ToList();
        }

        public void ShowTask()
        {
            FlyoutDocumentManagerService.ShowNewEntityDocument<ResourceTaskModel>(this, task => task.Id = 0);
        }
        public void Upload()
        {
            FlyoutDocumentManagerService.ShowNewEntityDocument<UploadResourceModel>(this, task => { });
        }

        public void Publish()
        {
            ResourceModel[] temp = new ResourceModel[Selected.Count];
            Selected.CopyTo(temp, 0);

            var data = temp.Where(c => c.Status == (int)ResourceStatus.Uploaded).ToList();
            if (data.Any() && notifyService.Confirm("Are you sure you want to publish to the server") == System.Windows.Forms.DialogResult.Yes)
            {
                foreach (var item in data)
                {
                    item.PublishTime = DateTime.Now;
                    item.PublishUser = "habi.tv";
                    item.Url = "http://localhost/data/" + item.Name;

                    ResourceContainer.Default.Reflesh(item.Id, ResourceStatus.Pubilshed);
                }
            }
        }

        public void Download()
        {
            ResourceModel[] temp = new ResourceModel[Selected.Count];
            Selected.CopyTo(temp, 0);

            var data = temp.Where(c => c.Status == (int)ResourceStatus.Pubilshed).ToList();
            if (data.Any())
            {
                List<ResourceTaskModel> task = data.Select(c => new ResourceTaskModel
                {
                    Name = c.Name,
                    Extension = c.Extension,
                    Size = c.Size,
                    Icon = c.Extension.Trim('.'),
                    Id = Snowflake.Default.NextShortId(),
                    Status = (int)ResourceTaskStatus.Ready,
                    Progress = 0,
                    Type = (int)TaskType.Download,
                    Url = c.Url
                }).ToList();

                TaskContainer.Default.Add(task);
            }
        }
    }
}
